package com.example.views;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Fragment hTab01 = new WeChatBlankFragment();
    private Fragment hTab02 = new FriBlankFragment();
    private Fragment hTab03 = new FoundBlankFragment();
    private Fragment hTab04 = new MeBlankFragment();

    private LinearLayout hTabWeChat;
    private LinearLayout hTabFrd;
    private LinearLayout hTabContact;
    private LinearLayout hTabMe;

    private ImageView imageWeixin,imagepengyou,imagefaxian, imagewo;

    private FragmentManager fm=getSupportFragmentManager();;
    private FragmentTransaction transaction=fm.beginTransaction();
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initFragment();
        textView=findViewById(R.id.textView2);

        hTabWeChat =findViewById(R.id.tab_weixin);
        hTabFrd =findViewById(R.id.tab_pengyou);
        hTabContact =findViewById(R.id.tab_faxian);
        hTabMe =findViewById(R.id.tab_shezhi);
        initEvent();
        hideFragment(transaction);
        showfragment(0);
    }


    private void initFragment() {
        transaction.add(R.id.content_frame, hTab01);
        transaction.add(R.id.content_frame, hTab02);
        transaction.add(R.id.content_frame, hTab03);
        transaction.add(R.id.content_frame, hTab04);
        transaction.commit();
    }

    private void showfragment(int i){
        FragmentTransaction transaction=fm.beginTransaction();
        hideFragment(transaction);

        switch (i){
            case 0:
                textView.setText("微信");
                transaction.show(hTab01);
                //imageWeixin.setImageResource(R.drawable.img);
                break;

            case 1:
                textView.setText("朋友");
                transaction.show(hTab02);
                //imagepengyou.setImageResource(R.drawable.img_1);
                break;

            case 2:
                textView.setText("发现");
                transaction.show(hTab03);
                //imagefaxian.setImageResource(R.drawable.img_2);
                break;

            case 3:
                textView.setText("我");
                transaction.show(hTab04);
                //imagewo.setImageResource(R.drawable.img_3);
                break;

            default:
                break;
        }
        transaction.commit();
    }


    private void hideFragment(FragmentTransaction transaction) {
        transaction.hide(hTab01);
        transaction.hide(hTab02);
        transaction.hide(hTab03);
        transaction.hide(hTab04);
    }

    private void initEvent() {
        hTabWeChat.setOnClickListener(this);
        hTabFrd.setOnClickListener(this);
        hTabContact.setOnClickListener(this);
        hTabMe.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        //resetImage();
        if (v.getId() == R.id.tab_weixin) {
            showfragment(0);
        } else if (v.getId() == R.id.tab_pengyou) {
            showfragment(1);
        } else if (v.getId() == R.id.tab_faxian) {
            showfragment(2);
        } else if (v.getId() == R.id.tab_shezhi) {
            showfragment(3);
        } else {
            // default case, do nothing or handle other cases if needed
        }

    }
//    public void resetImage(){
//        imageWeixin.setImageResource(R.drawable.img);
//        imagepengyou.setImageResource(R.drawable.img_1);
//        imagefaxian.setImageResource(R.drawable.img_2);
//        imagewo.setImageResource(R.drawable.img_3);
//    }

}